package com.cn.intelligence.resetful.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbBrandMapper;
import com.cn.intelligence.mapper.TbItemMapper;
import com.cn.intelligence.pojo.TbBrand;
import com.cn.intelligence.pojo.TbBrandExample;
import com.cn.intelligence.pojo.TbItem;
import com.cn.intelligence.pojo.TbItemExample;
import com.cn.intelligence.resetful.service.IndexService;
import com.github.pagehelper.PageHelper;
/**
 * 首页信息机接口
 * @author Administrator
 *
 */
@Service
public class IndexServiceImpl implements IndexService{

	@Autowired
	public TbBrandMapper brandMapper;
	@Autowired
	public TbItemMapper itemMapper;
	

	/**
	 * 获取首页信息的brand图片 和产品初始信息
	 * @return
	 */
	@Override
	public Map<String, Object> getIndexListOfProduct(String value, int sex,int gongneng,int guo,String pingpai,int nianlin1,
			int nianlin2,int price1,int price2) {
		 Map<String, Object> map = new HashMap<String, Object>();		 
		// 查询brand信息
		TbBrandExample tbBrandExample = new TbBrandExample();
		List<TbBrand>  listbrand = brandMapper.selectByExample(tbBrandExample);
		map.put("ggtop", listbrand);
		
		
		// 查询商品信息
		TbItemExample itemExample = new TbItemExample();
		itemExample.setOrderByClause("stortorder DESC,sex DESC, updated DESC");
		TbItemExample.Criteria criteria = itemExample.createCriteria();
		criteria.andStatusNotEqualTo((byte)2);
		if (value != null && !"".equals(value)) {
			try {
				value = URLDecoder.decode(value, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andNameLike("%" + value + "%");
		}
		
		if (sex != 0 && !"".equals(sex)) {
			List<Integer> list = new ArrayList<Integer>();
			list.add(1);
			list.add(sex);
			criteria.andSexIn(list);
		}
		if (gongneng != 0 && !"".equals(gongneng)) {
			criteria.andSearchEqualTo(gongneng);
		}
		if (guo != 0 && !"".equals(guo)) {
			criteria.andSourceEqualTo(guo);
		}
		
		if (!"".equals(pingpai)) {
			try {
				pingpai = URLDecoder.decode(pingpai, "utf-8");
				System.out.print(pingpai);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andPinpaiLike("%" + pingpai + "%");
		}
		
		criteria.andUpageGreaterThan(nianlin1);
		criteria.andLowageLessThan(nianlin2);
		criteria.andSpriceBetween(price1, price2);
		
        // 分页查询
        PageHelper.startPage(1,10);
        
        TbItemExample itemExample2 = new TbItemExample();
		itemExample2.setOrderByClause("stortorder DESC,sex DESC, updated DESC");
		TbItemExample.Criteria criteria2 = itemExample2.createCriteria();
		criteria2.andStatusNotEqualTo((byte)2);
		
		if (value != null && !"".equals(value)) {
			try {
				value = URLDecoder.decode(value, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria2.andNameLike("%" + value + "%");	
			if (sex != 0 && !"".equals(sex)) {
				List<Integer> list2 = new ArrayList<Integer>();
				list2.add(1);
				list2.add(sex);
				criteria2.andSexIn(list2);
			}
			if (gongneng != 0 && !"".equals(gongneng)) {
				criteria2.andSearchEqualTo(gongneng);
			}
			if (!"".equals(pingpai)) {
				try {
					pingpai = URLDecoder.decode(pingpai, "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				criteria2.andPinpaiLike("%" + pingpai + "%");
			}
			if (guo != 0 && !"".equals(guo)) {
				criteria2.andSourceEqualTo(guo);
			}
			criteria2.andUpageGreaterThan(nianlin1);
			criteria2.andLowageLessThan(nianlin2);
			itemExample.or(criteria2); 
		}
        
        
        List<TbItem> tbItemList= itemMapper.selectByExampleWithBLOBs(itemExample);
        

       map.put("prolist", tbItemList);
	  return map;
	}
	@Override
	public Map<String, Object> geMoreProducts(String value,int page, int rows,int sex,int gongneng,int guo,String pingpai,int nianlin1,
			int nianlin2,int price1,int price2) {
		 Map<String, Object> map = new HashMap<String, Object>();	
		// 查询商品信息
		TbItemExample itemExample = new TbItemExample();
        // 分页查询
        PageHelper.startPage(page,rows);
        
		
		// 查询商品信息
		itemExample.setOrderByClause("stortorder DESC,sex DESC, updated DESC");
		TbItemExample.Criteria criteria = itemExample.createCriteria();
		criteria.andStatusNotEqualTo((byte)2);
		if (value != null && !"".equals(value)) {
			try {
				value = URLDecoder.decode(value, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andNameLike("%" + value + "%");
		}
		
		if (sex != 0 && !"".equals(sex)) {
			List<Integer> list = new ArrayList<Integer>();
			list.add(1);
			list.add(sex);
			criteria.andSexIn(list);
		}
		if (gongneng != 0 && !"".equals(gongneng)) {
			criteria.andSearchEqualTo(gongneng);
		}
		if (guo != 0 && !"".equals(guo)) {
			criteria.andSourceEqualTo(guo);
		}
		
		if (!"".equals(pingpai)) {
			try {
				pingpai = URLDecoder.decode(pingpai, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andPinpaiLike("%" + pingpai + "%");
		}
		
		criteria.andUpageGreaterThan(nianlin1);
		criteria.andLowageLessThan(nianlin2);
		criteria.andSpriceBetween(price1, price2);
		
        
        TbItemExample itemExample2 = new TbItemExample();
		itemExample2.setOrderByClause("stortorder DESC,sex DESC, updated DESC");
		TbItemExample.Criteria criteria2 = itemExample2.createCriteria();
		criteria2.andStatusNotEqualTo((byte)2);
		
		if (value != null && !"".equals(value)) {
			try {
				value = URLDecoder.decode(value, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria2.andNameLike("%" + value + "%");	
			if (sex != 0 && !"".equals(sex)) {
				List<Integer> list2 = new ArrayList<Integer>();
				list2.add(1);
				list2.add(sex);
				criteria2.andSexIn(list2);
			}
			if (gongneng != 0 && !"".equals(gongneng)) {
				criteria2.andSearchEqualTo(gongneng);
			}
			if (guo != 0 && !"".equals(guo)) {
				criteria2.andSourceEqualTo(guo);
			}
			if ( !"".equals(pingpai)) {
				try {
					pingpai = URLDecoder.decode(pingpai, "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				criteria2.andPinpaiLike("%" + pingpai + "%");
			}
			criteria2.andUpageGreaterThan(nianlin1);
			criteria2.andLowageLessThan(nianlin2);
			criteria.andSpriceBetween(price1, price2);
			itemExample.or(criteria2); 
		}
        
        
        List<TbItem> tbItemList= itemMapper.selectByExampleWithBLOBs(itemExample);
        
        
        //PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(tbItemList);
        map.put("prolist", tbItemList);
		return map;
	}

	public Map<String, Object> getIndexListOfProduct2( int show) {
		 Map<String, Object> map = new HashMap<String, Object>();		 
		// 查询brand信息
		TbBrandExample tbBrandExample = new TbBrandExample();
		List<TbBrand>  listbrand = brandMapper.selectByExample(tbBrandExample);
		map.put("ggtop", listbrand);
		
		
		// 查询商品信息
		TbItemExample itemExample = new TbItemExample();
		itemExample.setOrderByClause("updated DESC");
		TbItemExample.Criteria criteria = itemExample.createCriteria();
		criteria.andIsShowEqualTo(show);	
        // 分页查询
        PageHelper.startPage(1,10);
        List<TbItem> tbItemList= itemMapper.selectByExampleWithBLOBs(itemExample);
        

       map.put("prolist", tbItemList);
	  return map;
	}
	@Override
	public Map<String, Object> geMoreProducts2(int page, int rows,int show) {
		 Map<String, Object> map = new HashMap<String, Object>();	
		// 查询商品信息
		TbItemExample itemExample = new TbItemExample();
        // 分页查询
        PageHelper.startPage(page,rows);
        
		
		// 查询商品信息
		itemExample.setOrderByClause("updated DESC");
		TbItemExample.Criteria criteria = itemExample.createCriteria();
		criteria.andIsShowEqualTo(show);	
        
        List<TbItem> tbItemList= itemMapper.selectByExampleWithBLOBs(itemExample);
        
        
        //PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(tbItemList);
        map.put("prolist", tbItemList);
		return map;
	}

	
}
